package com.health.work.activity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.health.work.R;
import com.health.work.model.QuestionBean;
import com.health.work.parse.CommonParse;
import com.health.work.utils.DefaultJSONData;
import com.health.work.utils.HEALTHPASSPORT;
import com.health.work.utils.RequestParamtersUtil;
import com.health.work.utils.TipsUtil;
import com.health.work.utils.UserInfoUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * 问题详情
 * 
 * @author star
 * 
 */
public class ProblemDetailsActivity extends Activity implements OnClickListener {

	private final static int SUCCESS = 0;
	private final static int FAILE = 1;
	private final static int ERROR = 2;
	private static final int TIPS = 3;
	private static final int SHOWQUESTION = 4;

	private Button back, submit, evluate;
	private int pos;
	private int qusetionId;
	private TextView condition_des;
	private QuestionBean bean;
	protected ProgressDialog dialog;

	private String EvaluationState = "好评";
	private String Evaluation = "好评";
	private TextView trusteeship;
	private boolean isTrusteeShip = false;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUCCESS:
				TipsUtil.showAlert(ProblemDetailsActivity.this, null, "数据请求成功！");
				break;
			case FAILE:
				TipsUtil.showAlert(ProblemDetailsActivity.this, null, "数据请求失败！");
				break;
			case ERROR:
				TipsUtil.showAlert(ProblemDetailsActivity.this, null, "未知错误！");
				break;
			case TIPS:
				TipsUtil.showAlert(ProblemDetailsActivity.this, null,
						errorMessage);
				break;
			case SHOWQUESTION:
				// 显示 questionDetailList 的数据 需要用到listview and adpter
				// adapter=new QuestionListAdapter(ProblemDetailsActivity.this,
				// questionDetailList, order_list);
				// order_list.setAdapter(adapter);
				break;
			default:
				break;
			}

		}

	};
	protected String errorMessage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.problem_details_layout);
		pos = getIntent().getIntExtra("postion", 0);
		qusetionId = getIntent().getIntExtra("id", 0);
		initPages();
		checkTrusteeShip();
	}

	private void initPages() {
		trusteeship = (TextView) this.findViewById(R.id.btnShare);
		condition_des = (TextView) this.findViewById(R.id.condition_des);
		back = (Button) this.findViewById(R.id.btnBack);
		submit = (Button) this.findViewById(R.id.submit);
		evluate = (Button) this.findViewById(R.id.evluate);
		back.setOnClickListener(this);
		submit.setOnClickListener(this);
		evluate.setOnClickListener(this);
		trusteeship.setOnClickListener(this);
		bean = HealthInquiryActivity.questionList.get(pos);
		if (bean != null) {
			condition_des.setText(bean.Question);
		}
		request(qusetionId);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnBack:
			finish();
			break;
		case R.id.submit:
			Intent i = new Intent(this, AskActivity.class);
			startActivity(i);
			break;
		case R.id.evluate:
			evalution(qusetionId);
			break;
		case R.id.btnShare:// 聘请专家
			Intent intent;
			if(isTrusteeShip){
				intent = new Intent(ProblemDetailsActivity.this,
					TrusteeShipActivity.class);
			startActivity(intent);
			}else{
				intent = new Intent(ProblemDetailsActivity.this,
						ExpertListActivity.class);
				startActivity(intent);
			}
			
		default:
			break;
		}

	}

	private DefaultJSONData parse = new DefaultJSONData() {

		@Override
		public void parse(String result) throws JSONException {
			Log.e("GetQuestionDetail result", result.toString());
			if ("0".equals(result)) {
				errorMessage = "获取问题详细信息，暂无数据！";
				handler.sendEmptyMessage(TIPS);
			}
		}

		@Override
		public void parse(JSONObject object) throws JSONException {
			Log.e("GetQuestionDetail object", object.toString());
		}

		@Override
		public void parse(JSONArray array) throws JSONException {
			Log.e("GetQuestionDetail array", array.toString());
		}
	};

	private CommonParse parseData;

	protected void request(final int Id) {
		final Context context = this;
		AsyncTask<Void, Void, Void> asyncLoad = new AsyncTask<Void, Void, Void>() {

			@Override
			protected void onPreExecute() {
				if (dialog != null) {

				} else {
					dialog = ProgressDialog.show(context, "", "正在加载数据，请稍候...");
					dialog.setCancelable(true);
				}
				// if(typeParse==null){
				// typeParse=new TypeParse();
				// }

			}

			@Override
			protected Void doInBackground(Void... params) {
				new RequestParamtersUtil().GetQuestionDetail(Id, parse);
				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				dialog.dismiss();
				dialog = null;
			}
		};
		asyncLoad.execute(null);
	}

	protected void evalution(final int Id) {
		final Context context = this;
		AsyncTask<Void, Void, Void> asyncLoad = new AsyncTask<Void, Void, Void>() {
			@Override
			protected void onPreExecute() {
				if (dialog != null) {

				} else {
					dialog = ProgressDialog.show(context, "", "正在加载数据，请稍候...");
					dialog.setCancelable(true);
				}
				if (parseData == null) {
					parseData = new CommonParse();
				}
			}

			@Override
			protected Void doInBackground(Void... params) {
				new RequestParamtersUtil().Evaluation(Id, EvaluationState,
						Evaluation, parseData);
				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				dialog.dismiss();
				dialog = null;
				if ("1".equals(parseData.resultData)
						|| "9999".equals(parseData.resultData)) {
					errorMessage = "评价失败！";
					handler.sendEmptyMessage(TIPS);
				} else if ("0".equals(parseData.resultData)) {
					errorMessage = "评价成功！";
					handler.sendEmptyMessage(TIPS);
				}
			}
		};
		asyncLoad.execute(null);
	}

	private void checkTrusteeShip() {
		final String phoneNum = UserInfoUtils.getUserProperty(this,
				HEALTHPASSPORT.MOBILEPHONE);
		Thread thread = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				new RequestParamtersUtil().GetTrusteeship(phoneNum,
						new DefaultJSONData() {

							@Override
							public void parse(String result)
									throws JSONException {
								// TODO Auto-generated method stub
								Log.e("GetTrusteeshipStr", result);
								if ("0".equals(result)) {// 尚未托管
									isTrusteeShip = false;
								} else if ("1".equals(result)) {// 待专家确认
									isTrusteeShip = true;
								} else {// 托管成功
									isTrusteeShip = true;
								}
							}

							@Override
							public void parse(JSONObject object)
									throws JSONException {
								// TODO Auto-generated method stub
								Log.e("GetTrusteeshipObj", object.toString());
							}

							@Override
							public void parse(JSONArray array)
									throws JSONException {
								// TODO Auto-generated method stub
								Log.e("GetTrusteeshipObj", array.toString());
							}
						});
			}
		});
		thread.start();
	}

}
